package com.xzd.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzd.common.result.Result;
import com.xzd.model.system.SysRole;
import com.xzd.model.vo.SysRoleQueryVo;
import com.xzd.system.exception.GuiguException;
import com.xzd.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("findAll")
    public Result findAllRole(){
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }
    @ApiOperation(value = "获取全部角色列表")
    @DeleteMapping("remopve/{id}")
    public Result removeRow(@PathVariable Long id){
        boolean isSuccess = sysRoleService.removeById(id);
        if(isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result findQueryRow(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo){

        Page<SysRole>pageParam =new Page<>(page,limit);

        IPage<SysRole>pagesModel=sysRoleService.selectPage(pageParam,sysRoleQueryVo);

        return Result.ok(pagesModel);
    }

    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result saveRole(@RequestBody SysRole sysRole)
    {
        boolean isSuccess = sysRoleService.save(sysRole);
        if(isSuccess)
        {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("根据id查询")
    @PostMapping("findRoleById/{id}")
    public Result findRoleById(@PathVariable Long id)
    {
       SysRole sysRole=sysRoleService.getById(id);
       return Result.ok(sysRole);
    }

    @ApiOperation("修改角色")
    @PostMapping("updateRole")
    public Result updateRole(@RequestBody SysRole sysRole)
    {
       boolean isSuccess=sysRoleService.save(sysRole);
       if(isSuccess)
       {
           return Result.ok();
       }
       return Result.fail();
    }

    @ApiOperation("批量删除")
    @PostMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long>ids)
    {
        sysRoleService.removeByIds(ids);
        return  Result.ok();
    }

    @ApiOperation("异常处理")
    @PostMapping("mannerException")
    public Result mannerException()
    {
        try {
            int a=10/0;
        }catch (Exception e){
            //手动抛出异常
            throw new GuiguException(200001,"执行了自定义异常处理");
        }


        return Result.ok();
    }


}
